﻿using DirectSell.IMongoDAL.Manager.MessageManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.Message;
using DirectSell.Model.Query;
using MongoDB.Driver;

namespace DirectSell.MongoDAL.Manager.MessageManage
{
    /// <summary>
    /// 留言管理Mongo访问层
    /// </summary>
    public class MessageManageRepository : MongoBaseRepository, IMessageManageRepository
    {
        #region 构造函数
        public MessageManageRepository() : base("Messages")
        {
        }
        #endregion

        #region 我发出的留言
        /// <summary>
        /// 我发出的留言
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<IList<Messages>> AlreadyMessage(BaseCondition condition)
        {
            //根据时间排序
            var sort = Builders<Messages>.Sort.Ascending("State");
            var list = new List<FilterDefinition<Messages>>();
            list.Add(Builders<Messages>.Filter.Eq("SendCode", "admin"));
            //>开始时间 <结束时间
            if (!string.IsNullOrEmpty(condition.startTime) && !string.IsNullOrEmpty(condition.endTime))
            {
                list.Add(Builders<Messages>.Filter.Gte("CreateDate", condition.startTime));
                list.Add(Builders<Messages>.Filter.Lte("CreateDate", condition.endTime));
            }
            //Or条件
            if (!string.IsNullOrEmpty(condition.SerachCondition))
            {
                var list_or = new List<FilterDefinition<Messages>>();
                list_or.Add(Builders<Messages>.Filter.Regex("Title", condition.SerachCondition));
                list_or.Add(Builders<Messages>.Filter.Regex("MContent", condition.SerachCondition));
                list_or.Add(Builders<Messages>.Filter.Regex("SendCode", condition.SerachCondition));
                list.Add(Builders<Messages>.Filter.Or(list_or));
            }
            var filter = Builders<Messages>.Filter.And(list);
            return await base.FindListByPageAsync<Messages>(filter,condition.pageIndex,condition.pageSize ,null, sort);
        }
        #endregion

        #region 发送给我的留言
        /// <summary>
        /// 发送给我的留言
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<IList<Messages>> MyMessage(BaseCondition condition)
        {
            //根据时间排序
            var sort = Builders<Messages>.Sort.Ascending("State");
            var list = new List<FilterDefinition<Messages>>();
            list.Add(Builders<Messages>.Filter.Eq("ReciveCode", "admin"));
            //>开始时间 <结束时间
            if (!string.IsNullOrEmpty(condition.startTime) && !string.IsNullOrEmpty(condition.endTime))
            {
                list.Add(Builders<Messages>.Filter.Gte("CreateDate", condition.startTime));
                list.Add(Builders<Messages>.Filter.Lte("CreateDate", condition.endTime));
            }
            //Or条件
            if (!string.IsNullOrEmpty(condition.SerachCondition))
            {
                var list_or = new List<FilterDefinition<Messages>>();
                list_or.Add(Builders<Messages>.Filter.Regex("Title", condition.SerachCondition));
                list_or.Add(Builders<Messages>.Filter.Regex("MContent", condition.SerachCondition));
                list_or.Add(Builders<Messages>.Filter.Regex("ReciveCode", condition.SerachCondition));
                list.Add(Builders<Messages>.Filter.Or(list_or));
            }
            var filter = Builders<Messages>.Filter.And(list);
            return await base.FindListByPageAsync<Messages>(filter,condition.pageIndex,condition.pageSize, null, sort);
           
        }
        #endregion
    }
}
